package com.wjk.easy.uhf;

import android.content.Context;


import com.android.hdhe.uhf.reader.UhfReader;
import com.wjk.easy.utils.UHFUtil;

public class UHFController {
    private UhfReader mReader;
    InventoryThread thread;

    public UHFController(Context context) {
        // init sound pool
        UHFUtil.initSoundPool(context);
    }

    public void initUhf(final UHFLoadListener listener){
        new Thread(){
            @Override
            public void run() {
                mReader = UhfReader.getInstance();
                if (mReader != null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        mReader.setOutputPower(26);
                        mReader.setWorkArea(2);
                    }
                    //start inventory thread
                    thread = new InventoryThread(mReader);
                    thread.start();
                    listener.onLoadOk();
                }
            }
        }.start();
    }

    public void onDestroy() {
        if(thread!=null){
            thread.onDestroy();
        }
        if(mReader!=null){
            mReader.close();
        }
    }

    public void onPause() {

        if(thread!=null){
            thread.onPause();
        }
    }

    public void restart(){
        if(thread!=null){
            thread.restart();
        }
    }


    public void setUHFListener(EPCReadListener listener) {
        if(thread!=null){
            thread.setUHFListener(listener);
        }
    }

    public interface UHFLoadListener{
        void onLoadOk();
    }
}
